      SUBROUTINE TGLOC05(NTARGS,KSYSNAT,KCENNAT,TARGNAME,
     *     TSEC50,SCPOS,SCVEL,TARGUNIT,LUERR,IERR)
      IMPLICIT REAL*8 (A-H,O-Z)
C
C THIS ROUTINE IS PART OF THE TOSS TARGET LOCATION PACKAGE, TGLOC.  IT
C COMPUTES UNIT VECTORS FOR NOON ZENITH AND MIDNIGHT ZENITH TARGETS.
C
C VAR     DIM    TYPE I/O DESCRIPTION
C ---     ---    ---- --- -----------
C
C NTARGS   1      I*4  I  NUMBER OF TARGETS TO BE PROCESSED.
C                         IF ZERO OR NEGATIVE, NONE ARE PROCESSED.
C
C KSYSNAT  1      I*4  O  IDENTIFIES THE NATURAL COORDINATE SYSTEM'S
C                         ORIENTATION.  IS SET TO 1 TO INDICATE THE
C                         ORIENTATION IS MEAN EARTH EQUATOR AND
C                         EQUINOX OF 1950.0
C
C KCENNAT  1      I*4  O  IDENTIFIES THE NATURAL COORDINATE SYSTEM'S
C                         ORIGIN.  IS SET TO 9999 TO INDICATE THE ORIGIN
C                         IRRELEVANT.
C
C TARGNAME NTARGS CH*16 I NAME OF THE ZENITH TARGET.  VALID NAMES ARE
C                         LISTED IN TGLOC'S DESCRIPTION OF TARGNAME
C                         FOR TARGET TYPE 5.
C
C TSEC50   1      R*8  I  THE TIME ASSOCIATED WITH SCPOS AND SCVEL. TIME
C                         IS NEEDED BECAUSE THE SUN POSITION IS
C                         INVOLVED.  IN SECONDS SINCE 1/1/50, 0.0 HR
C
C SCPOS    3      R*8  I  THE SPACECRAFT POSITION VECTOR AT TIME TSEC50.
C                         THE S/C STATE IS NEEDED BECAUSE ZENITH DEPENDS
C                         ON THE TRAJECTORY.  MEAN OF 1950.0 IS ASSUMED.
C                         IN KM.
C
C SCVEL    3      R*8  I  THE SPACECRAFT VELOCITY VECTOR AT TIME TSEC50.
C                         MEAN OF 1950.0 IS ASSUMED.  IN KM/SEC.
C
C TARGUNIT 3,NTARGS R*8 O TARGUNIT(-,I) IS THE UNIT VECTOR OF THE I'TH
C                         TARGET.  IN MEAN OF 1950.0
C
C LUERR    1      I*4  I  FORTRAN UNIT NUMBER FOR ERROR MESSAGES.
C                          = 0/NEGATIVE, NO MESSAGES POSSIBLE.
C
C IERR     1      I*4  O  ERROR RETURN FLAG.
C                          = 0, NO ERROR.
C                          = OTHERWISE, ERROR.
C
C***********************************************************************
C
C BY C PETRUZZO, GSFC/742, 8/85.
C        MODIFIED....
C
C***********************************************************************
C
      PARAMETER NUMALG=2
      LOGICAL HAVEMIDZ(NUMALG),HAVENOONZ(NUMALG)
      REAL*8 ZNOON(3,NUMALG),ZMIDNT(3,NUMALG)
C
      CHARACTER*16
     *  NOONZ1/'NOONZENITH1'/, MIDZ1/'MIDZENITH1'/,   ! ALGORITHM 1
     *  NOONZ2/'NOONZENITH2'/, MIDZ2/'MIDZENITH2'/    ! ALGORITHM 2
C
      CHARACTER*16 TARGNAME(1),THISNAME
      REAL*8 TARGUNIT(3,1)
      REAL*8 SCPOS(3),SCVEL(3),TARG(3)
      REAL*8 HALFPI/ 1.570796326794897D0 /
      REAL*8 PI/ 3.141592653589793D0 /
      REAL*8 TWOPI/ 6.283185307179586D0 /
      REAL*8 DEGRAD/ 57.29577951308232D0 /
      LOGICAL CALLIT
C
      IBUG = 0
      LUBUG = 19
C
      IF(IBUG.NE.0) THEN
        WRITE(LUBUG,5001) NTARGS,PAKTIM50(TSEC50),SCPOS,SCVEL
        IF(NTARGS.GT.0) WRITE(LUBUG,5002) (I,TARGNAME(I),I=1,NTARGS)
 5001   FORMAT(' TGLOC05 DEBUG. NTARGS=',I4,'  TIME=',F13.6/,
     *       '     SCPOS = ',3G20.12/,
     *       '     SCVEL = ',3G20.12)
 5002   FORMAT('   I,TARGNAME(I)=',<NTARGS>(T20,3A/) )
        END IF
C
C
C ****************
C *  INITIALIZE  *
C ****************
C
      IERR = 0
      KSYSNAT = 1       ! ORIENTATION IS MEAN OF 1950.0
      KCENNAT = 9999    ! INDEPENDENT OF ORIGIN
      IF(NTARGS.LE.0) GO TO 9999
C
      DO IALG=1,NUMALG
        HAVEMIDZ(IALG) =  .FALSE.
        HAVENOONZ(IALG) = .FALSE.
        END DO
C
C ******************************
C *  LOOP THROUGH THE TARGETS  *
C ******************************
C
      DO 1000 ITARG = 1,NTARGS
C
C
C USE NAME TO DETERMINE ALGORITHM TO BE USED.
C
      THISNAME = TARGNAME(ITARG)
      IF(THISNAME.EQ.NOONZ1 .OR. THISNAME.EQ.MIDZ1) THEN
        JALG = 1
      ELSE IF(THISNAME.EQ.NOONZ2 .OR. THISNAME.EQ.MIDZ2) THEN
        JALG = 2
      ELSE
        IERR = 1
        TARGUNIT(1,ITARG) = 'ERROR'
        TARGUNIT(2,ITARG) = 'ERROR'
        TARGUNIT(3,ITARG) = 'ERROR'
        IF(LUERR.GT.0) WRITE(LUERR,9001) THISNAME
 9001   FORMAT(/,' TGLOC05. BAD NOON/MIDNIGHT ZENITH TARGET NAME ',
     *     'SUPPLIED. NAME=',A)
        GO TO 1000
        END IF
C
C
C COMPUTE THE TARGET DIRECTION, STORE IN INTERNAL ARRAY.
C
C    SET FLAG INDICATING NEED TO COMPUTE TARGET DIRECTION.  THIS IS
C    USEFUL IN CASE TARGETS ARE REPEATED IN THE TARGET ARRAY.
      CALLIT =
     *   ( THISNAME(1:4).EQ.'NOON' .AND. .NOT.HAVENOONZ(JALG) )     .OR.
     *   ( THISNAME(1:4).EQ.'MIDZ' .AND. .NOT.HAVEMIDZ(JALG)  )
C
      IF(CALLIT) THEN
        IF(JALG.EQ.1) THEN
C        ZENITH IS HALFWAY IN TIME BETWEEN SUNRISE AND SUNSET OR
C        SUNSET AND SUNRISE.
          CALL TGLOC05A(THISNAME,TSEC50,SCPOS,SCVEL,
     *            TIMTARG,TARG,LUERR,IER1)
        ELSE
C        ZENITH IS ON OR OPPOSITE SUN MERIDIAN
          CALL TGLOC05B(THISNAME,TSEC50,SCPOS,SCVEL,
     *            TIMTARG,TARG,LUERR,IER1)
          END IF
        IF(THISNAME(1:4) .EQ. 'NOON') THEN
          HAVENOONZ(JALG) = .TRUE.
          IF(IER1.EQ.0) THEN
            CALL VNORM(TARG,ZNOON(1,JALG))
          ELSE
            IERR = 1
            ZNOON(1,JALG) = 'ERROR'
            ZNOON(2,JALG) = 'ERROR'
            ZNOON(3,JALG) = 'ERROR'
            END IF
        ELSE
          HAVEMIDZ(JALG) =  .TRUE.
          IF(IER1.EQ.0) THEN
            CALL VNORM(TARG,ZMIDNT(1,JALG))
          ELSE
            IERR = 1
            ZMIDNT(1,JALG) = 'ERROR'
            ZMIDNT(2,JALG) = 'ERROR'
            ZMIDNT(3,JALG) = 'ERROR'
            END IF
          END IF
        END IF
C
C
C LOAD THE OUTPUT ARRAY
C
      IF(THISNAME(1:4) .EQ. 'NOON') THEN
        TARGUNIT(1,ITARG) = ZNOON(1,JALG)
        TARGUNIT(2,ITARG) = ZNOON(2,JALG)
        TARGUNIT(3,ITARG) = ZNOON(3,JALG)
      ELSE
        TARGUNIT(1,ITARG) = ZMIDNT(1,JALG)
        TARGUNIT(2,ITARG) = ZMIDNT(2,JALG)
        TARGUNIT(3,ITARG) = ZMIDNT(3,JALG)
        END IF
C
 1000 CONTINUE
C
 9999 CONTINUE
      RETURN
      END
